#pragma once

#include <list>

struct Msg
{
	byte buf[512];
	size_t len;
};

class Hal
{
public:
	Hal(void);
	virtual ~Hal(void);

	bool Init(CString fileName, DWORD idThread, UINT msg);
	virtual void Read(void* buf, DWORD len, DWORD& realLen);
	virtual bool Write(void* buf, DWORD len, DWORD& realLen);
private:
	static UINT ReadThread(LPVOID param);
	void ReadWorker();
	void AddMsg(byte* buf, size_t len);

public:
	void GetMsg(byte* buf, size_t& len);
	void PopMsg();

protected:
	virtual HANDLE CreateFile(CString fileName);
	virtual bool WriteReadEvent(HANDLE handle);

private:
	CString fileName_;
	DWORD idThread_;
	UINT msg_;
	HANDLE fileHandle_;
	HANDLE readEvent_;

	std::list<Msg> list_;

	CRITICAL_SECTION m_Section;
};
